package com.hfut.wxy.common;

import com.alibaba.fastjson.JSONObject;
import com.hfut.wxy.common.exception.WxyException;
import com.hfut.wxy.common.utils.RedisUtil;
import okhttp3.*;
import org.apache.http.client.methods.RequestBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

@Component
public class OkHttp {
    private static final String appId = "wx675eb6780ec1eb91";

    private static final String appSecret = "10f75b203d31c9bfa09009e9d171bf46";

    private OkHttpClient okHttpClient;


    public  OkHttpClient okHttpClient(){
        if (okHttpClient == null){
            okHttpClient = new OkHttpClient.Builder()
                    .readTimeout(60, TimeUnit.SECONDS)
                    .writeTimeout(60,TimeUnit.SECONDS)
                    .connectTimeout(60,TimeUnit.SECONDS)
                    .callTimeout(60,TimeUnit.SECONDS)
                    .build();
        }
        return okHttpClient;
    }

    public  JSONObject getOpenId(String code){
        String url = Constants.CODE_URL+"?appid="+appId+"&secret="+appSecret+"&grant_type=authorization_code&js_code="
                +code;
        Request request = new Request.Builder()
                .url(url)
                .get()
                .build();
        Response response;
        try {
            response = okHttpClient().newCall(request).execute();
            String re = Objects.requireNonNull(response.body()).string();
            JSONObject jsonObject = JSONObject.parseObject(Objects.requireNonNull(re));
            if (jsonObject.getInteger("errcode") != null && jsonObject.getInteger("errcode").equals(40163)){
                throw WxyException.of(jsonObject.getString("errmsg"));
            }
            return jsonObject;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public  JSONObject getVCode(String access_token,String image){
//        String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic"
        String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic"
                +"?access_token="+access_token;
        Request request = new Request.Builder()
                .url(url)
                .post(RequestBody.create("image="+image, MediaType.parse(org.springframework.http.MediaType.APPLICATION_FORM_URLENCODED_VALUE)))
                .build();
        Response response;
        try {
            response = okHttpClient().newCall(request).execute();
            String re = Objects.requireNonNull(response.body()).string();
            return JSONObject.parseObject(Objects.requireNonNull(re));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void access_token(){
        String url = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=6gNlhGDGhv5m0ClAVCN65Znv&client_secret=39ZAaOs8sQWzvZ7eVjnQXcFx9cQcNIwz";
        Request request = new Request.Builder()
                .url(url)
                .post(RequestBody.create("",MediaType.parse(org.springframework.http.MediaType.APPLICATION_FORM_URLENCODED_VALUE)))
                .build();
        Response response;
        try {
            response = okHttpClient().newCall(request).execute();
            String re = Objects.requireNonNull(response.body()).string();
            Constants.access_token = JSONObject.parseObject(re).getString("access_token");
            System.out.println(Constants.access_token);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
